package com.irdstudio.tdp.executor.core.plugin.gencode.dao;

import com.irdstudio.tdp.executor.core.plugin.gencode.vo.PaasDeployInfo;
import com.irdstudio.tdp.executor.core.plugin.util.POJOUtils;
import com.irdstudio.tdp.executor.core.plugin.util.ProxySQL;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PaasDeployInfoDao {
    private static final Logger logger = LoggerFactory.getLogger(PaasDeployInfoDao.class);
    /* 连接对象 */
    Connection conn = null;

    public PaasDeployInfoDao(Connection conn){
        this.conn = conn;
    }

    public PaasDeployInfo queryByPk(PaasDeployInfo paasDeployInfo) throws SQLException {
        List param = new ArrayList();
        String sql = new ProxySQL() {{
            SELECT("*");
            FROM("paas_deploy_info");
            WHERE("app_deploy_id = ?");
            param.add(paasDeployInfo.getAppDeployId());

        }}.toString();
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<PaasDeployInfo> list = new ArrayList<>();
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < param.size(); i++) {
                ps.setObject(i + 1, param.get(i));
            }
            rs = ps.executeQuery();
            if (rs.next()) {
                PaasDeployInfo bean = POJOUtils.generatePOJO(rs, PaasDeployInfo.class);
                list.add(bean);
            }

        } catch (Exception e) {
            throw new RuntimeException("query queryByPk is wrong", e);
        }
        return list.isEmpty() ? null : list.get(0);
    }


    public Integer queryMaxAppPort(PaasDeployInfo paasDeployInfo) throws SQLException {
        List param = new ArrayList();
        String sql = new ProxySQL() {{
            SELECT("max(app_port)  as app_port");
            FROM("paas_deploy_info");
            WHERE("ecs_ip = ?");
            param.add(paasDeployInfo.getEcsIp());
            WHERE("ecs_ip= ( select ecs_ip from paas_deploy_info where app_id = ? )");
            param.add(paasDeployInfo.getAppId());

        }}.toString();
        PreparedStatement ps = null;
        ResultSet rs = null;
        Integer maxPort = null;
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < param.size(); i++) {
                ps.setObject(i + 1, param.get(i));
            }
            rs = ps.executeQuery();
            if (rs.next()) {
                maxPort = rs.getInt("app_port");
            }

        } catch (Exception e) {
            throw new RuntimeException("query queryByPk is wrong", e);
        }
        return maxPort;
    }

    public boolean insertPaasDeployInfo(PaasDeployInfo paasDeployInfo) throws SQLException {
        List param = new ArrayList();
        String sql = new ProxySQL() {{
            INSERT_INTO("paas_deploy_info");
            List<Object[]> vals = POJOUtils.getPOJOValues(paasDeployInfo);
            for (Object[] val : vals) {
                VALUES(val[0].toString(), "?");
                param.add(val[1]);
            }

        }}.toString();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < param.size(); i++) {
                ps.setObject(i + 1, param.get(i));
            }
            int re = ps.executeUpdate();
            if (re == 1) {
                return true;
            }

        } catch (Exception e) {
            throw new RuntimeException("query queryByPk is wrong", e);
        }
        return false;
    }

    public List<PaasDeployInfo> queryAllByCondition(PaasDeployInfo paasDeployInfo) throws SQLException {
        List param = new ArrayList();
        String sql = new ProxySQL() {{
            SELECT("*");
            FROM("paas_deploy_info");
            List<Object[]> vals = POJOUtils.getPOJOValues(paasDeployInfo);
            for (Object[] val : vals) {
                WHERE(String.format("%s = ?", val[0].toString()));
                param.add(val[1]);
            }

        }}.toString();
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<PaasDeployInfo> list = new ArrayList<>();
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < param.size(); i++) {
                ps.setObject(i + 1, param.get(i));
            }
            rs = ps.executeQuery();
            if (rs.next()) {
                PaasDeployInfo bean = POJOUtils.generatePOJO(rs, PaasDeployInfo.class);
                list.add(bean);
            }

        } catch (Exception e) {
            throw new RuntimeException("query queryAllByCondition is wrong", e);
        }
        return list;
    }

    public Integer batchDeleteByAppIds(List<String> batchList) throws SQLException {
        List param = new ArrayList();
        String sql = new ProxySQL() {{
            DELETE_FROM("paas_deploy_info");
            List<String> qus = new ArrayList<>();
            for (String val : batchList) {
                qus.add("?");
                param.add(val);
            }
            WHERE(String.format("app_id in ( %s )", StringUtils.join(qus.toArray(new String[]{}), ",")));

        }}.toString();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < param.size(); i++) {
                ps.setObject(i + 1, param.get(i));
            }
            int re = ps.executeUpdate();
            return re;

        } catch (Exception e) {
            throw new RuntimeException("query queryByPk is wrong", e);
        }
    }

}
